const maxProfit = function (prices) {
  let n = prices.length;
  let buy = -prices[0];
  let sell = 0;
  let profit_freeze = 0;
  for (let i = 1; i < n; i++) {
    let temp = sell;
    sell = Math.max(sell, buy + prices[i]);
    buy = Math.max(buy, profit_freeze - prices[i]);
    profit_freeze = temp;
  }
  return sell;
};
